package com.ds.lens.data.controller;

import com.ds.lens.data.service.*;
import com.ds.lens.data.vo.BaseOutVO;
import com.ds.lens.data.vo.enums.VoMessageEnum;
import com.ds.lens.data.vo.factory.VOFactory;
import com.ds.lens.data.vo.in.PagedConditionInVO;
import com.ds.lens.data.vo.in.User;
import com.ds.lens.data.vo.in.v2.LensMonitorTagsInVO;
import com.ds.lens.data.vo.in.v2.monitor.*;
import com.ds.lens.data.vo.in.v2.monitor.state.LensMonitorStateInVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author WeiShaoying
 * @date 2019/11/6 下午2:32
 */
@RestController
@Api(value = "Lens monitor controller")
@RequestMapping(produces = {"application/json;charset=UTF-8"})
public class LensMonitorController {

    @Autowired
    LensMonitorService lensMonitorService;

    @Autowired
    LensMetricService lensMetricService;

    @Autowired
    LensMonitorMuteService lensMonitorMuteService;

    @Autowired
    LensMonitorStateService lensMonitorStateService;

    @Autowired
    LensExternalEventService lensExternalEventService;

    @ApiOperation(value = "Wake up monitor")
    @GetMapping(value = "/lens/monitor/wake")
    public BaseOutVO wakeUpMonitorByMonitorId(@RequestParam("monitorId") Long monitorId) {
        lensMonitorService.wakeUpMonitorByMonitorId(monitorId);
        return VOFactory.getSuccessBaseOutVO();
    }

    @ApiOperation(value = "list monitor states")
    @PostMapping(value = "/lens/monitor/states")
    public BaseOutVO listMonitorState(@RequestBody @Valid PagedConditionInVO<LensMonitorStateInVO> inVO,
                                      @RequestAttribute(value = "user") User user) {
        return VOFactory.getSuccessBaseOutVO(lensMonitorStateService.pageMonitorStateRecords(inVO, user));
    }

    @ApiOperation(value = "get filters")
    @PostMapping(value = "/lens/monitor/filters")
    public BaseOutVO getFilters(@RequestBody @Valid MetricFilterInVO metricFilterInVO) {
        return VOFactory.getSuccessBaseOutVO(lensMetricService.queryFilters(metricFilterInVO.getIntegrationId(),
                metricFilterInVO.getApplicationId(), metricFilterInVO.getTierId(), metricFilterInVO.getMetricName()));
    }

    @ApiOperation(value = "get group by fields")
    @PostMapping(value = "/lens/monitor/group/by/fields")
    public BaseOutVO getGroupByFields(@RequestBody @Valid MetricFilterInVO metricFilterInVO) {
        return VOFactory.getSuccessBaseOutVO(lensMonitorService.getGroupByFields(metricFilterInVO.getIntegrationId(),
                metricFilterInVO.getApplicationId(), metricFilterInVO.getMetricName()));
    }

    @ApiOperation(value = "save monitor")
    @PostMapping(value = "/lens/monitor/save")
    public BaseOutVO saveMonitor(@RequestBody @Valid LensMonitorInVO inVO,
                                 @RequestAttribute(value = "user") User user) {
        return VOFactory.getSuccessBaseOutVO(lensMonitorService.saveMonitor(inVO, user));
    }

    @ApiOperation(value = "update monitor")
    @PostMapping(value = "/lens/monitor/update")
    public BaseOutVO updateMonitor(@RequestBody @Valid LensMonitorUpdateInVO inVO,
                                   @RequestAttribute(value = "user") User user) {
        return VOFactory.getSuccessBaseOutVO(lensMonitorService.updateMonitor(inVO, user));
    }

    @ApiOperation(value = "mute monitor")
    @PostMapping(value = "/lens/monitor/mute")
    public BaseOutVO muteMonitor(@RequestBody @Valid LensMonitorMuteInVO inVO,
                                 @RequestAttribute(value = "user") User user) {
        return VOFactory.getSuccessBaseOutVO(lensMonitorMuteService.muteMonitor(inVO, user));
    }

    @ApiOperation(value = "resolve monitor")
    @GetMapping(value = "/lens/monitor/resolve")
    public BaseOutVO resolveMonitor(@RequestParam("ids") List<Long> ids,
                                    @RequestAttribute(value = "user") User user) {
        return VOFactory.getSuccessBaseOutVO(lensMonitorService.resolveMonitorState(ids, user));
    }

    @ApiOperation(value = "delete monitor")
    @GetMapping(value = "/lens/monitor/delete")
    public BaseOutVO deleteMonitor(@RequestParam("ids") List<Long> ids,
                                   @RequestAttribute(value = "user") User user) {
        return VOFactory.getSuccessBaseOutVO(lensMonitorService.deleteMonitor(ids, user));
    }

    @ApiOperation(value = "getAllTags")
    @PostMapping(value = "/lens/monitor/getAllTags")
    public BaseOutVO getAllTags(@RequestBody @Valid LensMonitorTagsInVO inVO) {
        return VOFactory.getSuccessBaseOutVO(lensMonitorService.getAllTags(inVO.getApplicationId()));
    }

    @ApiOperation(value = "list monitor")
    @PostMapping(value = "/lens/monitor/list")
    public BaseOutVO listMonitor(@RequestBody @Valid PagedConditionInVO<LensMonitorListInVO> inVO,
                                 @RequestAttribute(value = "user") User user) {
        return VOFactory.getSuccessBaseOutVO(lensMonitorService.listMonitor(inVO, user));
    }

    @ApiOperation(value = "get monitor detail")
    @GetMapping(value = "/lens/monitor/get")
    public BaseOutVO getMonitorDetails(@RequestParam("id") Long id,
                                       @RequestAttribute(value = "user") User user) {
        return VOFactory.getSuccessBaseOutVO(lensMonitorService.getMonitorDetails(id, user));
    }

    @ApiOperation(value = "get CTI detail")
    @GetMapping(value = "/lens/monitor/cti")
    public BaseOutVO CTI(@RequestParam("sessionId") String sessionId) {
        if (StringUtils.isEmpty(sessionId)) {
            return VOFactory.getBaseOutVO(VoMessageEnum.FAILED.getCode(), "sessionId can not be empty");
        }
        return VOFactory.getSuccessBaseOutVO(lensMonitorService.findCITBySessionId(sessionId));
    }

}
